﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;

namespace ServBus.ListForm2010.InfoBusFormWebPart
{
    [ToolboxItemAttribute(false)]
    public class InfoBusFormWebPart : WebPart
    {
        // 当更改可视 Web 部件项目项时，Visual Studio 可能会自动更新此路径。
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/ServBus.ListForm2010/InfoBusFormWebPart/InfoBusFormWebPartUserControl.ascx";

        protected override void CreateChildControls()
        {
            InfoBusFormWebPartUserControl control = Page.LoadControl(_ascxPath) as InfoBusFormWebPartUserControl;
            if (control != null)
            {
                control.webpart = this;
            }
            Controls.Add(control);
        }

        //[Category("Custom Properties")]
        //[DefaultValue(c_MyIntDefault)]
        //[WebPartStorage(Storage.Personal)]
        //[FriendlyNameAttribute("Custom Integer")]
        //[Description("Type an integer value.") ]
        //[Browsable(true)]

        #region Properties

        [DefaultValue(true)]
        [WebBrowsable(true)]
        [WebDescription("UrlMode")]
        [WebDisplayName("UrlMode")]
        [Personalizable(PersonalizationScope.Shared)]
        public bool UrlMode { get; set; }

        [Flags]
        public enum wpmEnum { New, Edit, Display };

        [WebBrowsable(true)]
        [WebDescription("WebPartMode")]
        [WebDisplayName("WebPartMode")]
        [Personalizable(PersonalizationScope.Shared)]
        public wpmEnum WebPartMode { get; set; }

        [WebBrowsable(true)]
        [WebDescription("SiteName")]
        [WebDisplayName("SiteName")]
        [Personalizable(PersonalizationScope.Shared)]
        public string SiteName { get; set; }

        [WebBrowsable(true)]
        [WebDescription("WebName")]
        [WebDisplayName("WebName")]
        [Personalizable(PersonalizationScope.Shared)]
        public string WebName { get; set; }

        public string ListId { get; set; }
        public string ContentTypeId { get; set; }

        [WebBrowsable(true)]
        [WebDescription("ItemID")]
        [WebDisplayName("ItemID")]
        [Personalizable(PersonalizationScope.Shared)]
        public string ItemID { get; set; }

        #endregion

        public override EditorPartCollection CreateEditorParts()
        {

            EditorPartCollection baseParts = base.CreateEditorParts();

            List<EditorPart> editorParts = new List<EditorPart>(1);

            EditorPart part = new MyEditorPart();

            part.ID = this.ID + "_tagValueEditor";

            part.Title = "InfoPath Form";

            editorParts.Add(part);

            return new EditorPartCollection(baseParts, editorParts);

        }
    }



    #region EditorPart

    public class MyEditorPart : EditorPart
    {
        private DropDownList ddl_List;
        private DropDownList ddl_ContentType;
        

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            ddl_List = new DropDownList();
            Label lbl_List = new Label();
            lbl_List.Text = "<strong>Choose List or Document Library:</strong><br />";
            Guid siteId = SPContext.Current.Site.ID;
            Guid webId = SPContext.Current.Web.ID;
            using (SPSite site = new SPSite(siteId))
            {
                using (SPWeb web = site.OpenWeb(webId))
                {
                    foreach (SPList list in web.Lists)
                    {
                        ListItem listItem = new ListItem(list.Title, list.ID.ToString());
                        ddl_List.Items.Add(listItem);
                    }
                }
            }
            ddl_List.SelectedIndexChanged+=new EventHandler(ddl_List_SelectedIndexChanged);
            ddl_List.AutoPostBack = true;
            Controls.Add(lbl_List);
            Controls.Add(ddl_List);

            ddl_ContentType = new DropDownList();
            Label lbl_ContentType = new Label();
            lbl_ContentType.Text = "<strong>Choose ContentType:</strong><br />";

            Controls.Add(lbl_ContentType);
            Controls.Add(ddl_ContentType);
        }

        protected void ddl_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid siteId = SPContext.Current.Site.ID;
            Guid webId = SPContext.Current.Web.ID;
            using (SPSite site = new SPSite(siteId))
            {
                using (SPWeb web = site.OpenWeb(webId))
                {
                    foreach (SPList list in web.Lists)
                    {
                        if (list.ID.ToString() == ddl_List.SelectedValue)
                        {
                            foreach (SPContentType contentType in list.ContentTypes)
                            {
                                ListItem listItem = new ListItem(contentType.Name);
                                ddl_ContentType.Items.Add(listItem);
                            }
                        }
                    }
                }
            }
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            InfoBusFormWebPart webPart = WebPartToEdit as InfoBusFormWebPart;
            if (webPart != null)
            {
                webPart.ListId = ddl_List.SelectedValue.ToString();
                webPart.ContentTypeId = ddl_ContentType.SelectedValue.ToString();
            }
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            InfoBusFormWebPart webPart = WebPartToEdit as InfoBusFormWebPart;
            if (webPart != null)
            {
                ddl_List.SelectedValue = webPart.ListId;
                ddl_ContentType.SelectedValue = webPart.ContentTypeId;
            }
        }
    }
    #endregion
}
